package com.liang.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.pojo.CarItem;
import com.liang.pojo.Gorillaceo;
import com.liang.pojo.OrderItem;
import com.liang.pojo.Orders;
import com.liang.service.OrderItemService;
import com.liang.service.OrdersService;

@Controller
public class OrdersController {
	
	@Autowired
	OrdersService ordersService;
	@Autowired
	OrderItemService orderItemService;
	
	@RequestMapping("/orders/orderss")
	public String orders() {
		return "orders/orders";
	}
	@RequestMapping("/orders/success")
	public String success(@RequestParam("oid")int oid,Model model) {
		Orders orders = ordersService.selectOrdersByOid(oid);
		orders.setOrderTime(DateFormat.getDateTimeInstance().format(new Date()));orders.setState(1);
		ordersService.updateOrders(orders);
		model.addAttribute("orders", orders);
		return "orders/success";
	}
	@RequestMapping("/orders/fukuan")
	public String fukuan(@RequestParam("oid")int oid,@RequestParam("total")double total,Model model) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("oid", oid);map.put("total", total);
		model.addAttribute("map", map);
		return "orders/saoma";
	}
	@RequestMapping("/orders/caozuo")
	@ResponseBody
	public int caozuo(@RequestParam("oid")int oid,Model model) {
		Orders orders = ordersService.selectOrdersByOid(oid);
		orders.setState(orders.getState()+1);
		ordersService.updateOrders(orders);
		return orders.getState();
	}
	  
	@RequestMapping("/orders/insertOrders") 
	@ResponseBody
	public Object insertOrders(Gorillaceo gorillaceo,HttpServletRequest request) {
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		int math=(int)(Math.random()*100000);
		System.out.println(math);
		map.put("oid", math);
		map.put("ordertime", DateFormat.getDateTimeInstance().format(new Date()));
		double total=0;
		for (CarItem carItem:gorillaceo.getCarItemList()) {
			total+=carItem.getSubtotal();
			map.put("sid", carItem.getStore().getSid());
			map.put("count", carItem.getCount());
			map.put("subtotal", carItem.getSubtotal());
			System.out.println(map);
			orderItemService.insertOrderItem(map);
		}
		map.put("total",total);
		map.put("state",0);
		map.put("address", "大园路凤翔新城");
		map.put("uid", (int)request.getSession().getAttribute("uid"));
		System.out.println(map);
		ordersService.insertOrders(map); 
		request.getSession().setAttribute("ordersNum", (int)request.getSession().getAttribute("ordersNum")+1);
		return map; 
	}
	
	@RequestMapping("/orders/insertOrdersOne") 
	public String insertOrdersOne(OrderItem orderItem,HttpServletRequest request) {
		System.out.println(orderItem);
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		int math=(int)(Math.random()*100000);
		map.put("oid", math);
		map.put("ordertime", DateFormat.getDateTimeInstance().format(new Date()));
		map.put("sid", orderItem.getSid());
		map.put("count", orderItem.getCount());
		map.put("subtotal",orderItem.getSubtotal());
		map.put("total",orderItem.getSubtotal());
		map.put("state",0);
		map.put("address", "大园路凤翔新城");
		map.put("uid", (int)request.getSession().getAttribute("uid"));
		orderItemService.insertOrderItem(map);
		ordersService.insertOrders(map); 
		request.getSession().setAttribute("ordersNum", (int)request.getSession().getAttribute("ordersNum")+1);
		request.setAttribute("map", map);
		return "orders/saoma"; 
	}
	
	@ResponseBody
	@RequestMapping("/orders/selectAllOrdersByUid")
	public Object selectAllOrdersByUid(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,HttpServletRequest request) {
		// 这个必须放在查询条件的前面，最好第一行
		PageHelper.startPage(pageNum, 12);
		
		int uid=(int)request.getSession().getAttribute("uid");
		List<Orders> allOrdersByUid = ordersService.selectAllOrdersByUid(uid);
		for (Orders orders : allOrdersByUid) {
			orders.setOrderItems(orderItemService.selectAllOrderItemByOid(orders.getOid()));
		}
		System.out.println(allOrdersByUid);
		PageInfo<Orders> pageInfo = new PageInfo<Orders>(allOrdersByUid);
		return pageInfo;
	}
	@RequestMapping("/orders/deleteBatch")
	@ResponseBody
	public int deleteBatch(Gorillaceo gorillaceo,HttpServletRequest request) {
		int res = JOptionPane.showConfirmDialog(null, "是否继续删除", "是否继续", JOptionPane.YES_NO_OPTION);
		int i = 0;
		if (res == JOptionPane.YES_OPTION) {
			int uid = (int) request.getSession().getAttribute("uid");
			gorillaceo.setUid(uid);
			i = ordersService.deleteOrdersByOid(gorillaceo); // 点击“是”后执行这个代码块
			request.getSession().setAttribute("ordersNum", (int)request.getSession().getAttribute("ordersNum")-i);
			int j=orderItemService.deleteOrderItemByOid(gorillaceo);
			System.out.println(j);
		} else {
			i = -1; // 点击“否”后执行这个代码块 }
		}
		return i;
	}
}
